package com.diabetes.controller.user;

import com.diabetes.constant.JwtClaimsConstant;
import com.diabetes.dto.UserLoginDTO;
import com.diabetes.dto.UserRegisterDTO;
import com.diabetes.dto.UserUpdateDTO;
import com.diabetes.entity.SugarRecord;
import com.diabetes.entity.User;
import com.diabetes.properties.JwtProperties;
import com.diabetes.result.Result;
import com.diabetes.service.UserService;
import com.diabetes.utils.JwtUtil;
import com.diabetes.vo.UserLoginVO;
import com.diabetes.vo.getBloodVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    //用户登录
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);

        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }
    //用户注册
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册：{}", userRegisterDTO);
        userService.register(userRegisterDTO);
        return Result.success();
    }
    //用户个人信息更改
    @PutMapping("/information")
    @ApiOperation("用户个人信息更改")
    public Result updateInformation(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("用户个人信息更改：{}", userUpdateDTO);
        userService.update(userUpdateDTO);
        return Result.success();
    }

    /**
     * 血糖信息插入
     * @param sugarRecord
     * @return
     */
    @PostMapping("/control")
    @ApiOperation("血糖信息记录插入")
    public Result bloodSearch(@RequestBody SugarRecord sugarRecord) {
        log.info("用户提交血糖信息为：{}",sugarRecord);
        userService.search(sugarRecord);
        return Result.success();
    }

    @GetMapping("/control/{ids}")
    public Result getblood(@PathVariable Integer ids) {
        log.info("用户传输的数值为{}",ids);
        SugarRecord sugarRecord = userService.get(ids);
        getBloodVO gb = getBloodVO.builder()
                .id(sugarRecord.getUserId())
                .blood(sugarRecord.getBloodSugar())
                .build();
        return Result.success(gb);
    }
}
